/*
 * SystemPropertyTableModel.java
 *
 * Created on 24 September 2004, 09:18
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.systemproperty;

import com.velocityme.client.gui.VectorTableModel;
import com.velocityme.valueobjects.SystemPropertyValue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 *
 * @author  Robert Crida Work
 */
public class SystemPropertyTableModel extends VectorTableModel {
    
    private boolean m_editable = false;
    
    /** Creates a new instance of SystemPropertyTableModel */
    public SystemPropertyTableModel() {
        super(new String[] {"Name", "Value"});
    }
    
    public Collection getElements() {
        Collection results = new ArrayList();
        for (int row = 0; row < getRowCount(); row++) {
            SystemPropertyValue value = (SystemPropertyValue)getValueAt(row,2); 
            value.setName((String)getValueAt(row,0));
            value.setValue((String)getValueAt(row,1));
            results.add(value);
        }
        return results;
    }
    
    public void addValues(Collection p_values) {
        m_dataRows.clear();
        Iterator i = p_values.iterator();
        while(i.hasNext()) {
            SystemPropertyValue valueObj = (SystemPropertyValue)i.next();
            String name = valueObj.getName();
            String value = valueObj.getValue();
            m_dataRows.addElement(new Object[]{ name, value, valueObj });
        }
        fireTableRowsInserted(m_dataRows.size() - 1, m_dataRows.size());
        if (p_values.isEmpty())
            addValue();
    }
    
    public void addValue() {
        m_dataRows.addElement(new Object[]{ new String(), new String(), new SystemPropertyValue() });
        fireTableRowsInserted(m_dataRows.size(), m_dataRows.size());
    }
    
    public void setEditable(boolean editable) {
        m_editable = editable;
    }
    
    public boolean isCellEditable(int row, int col) {
        return m_editable;
    }
    
    public void setValueAt(Object value, int row, int col) {
        ((Object[])m_dataRows.get(row))[col] = value;
        fireTableDataChanged();
    }
    
}
